package com.company.bookstore.service.impl;

import com.company.bookstore.mapper.book_module.BookMapper;
import com.company.bookstore.mapper.dataMapper.DataMapper;
import com.company.bookstore.service.data_module.DataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

@Service
public class DataServiceImpl implements DataService {

    @Resource
    DataMapper dataMapper;

    @Resource
    BookMapper bookMapper;

    @Override
    public Map<String, Object> getAllData(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("totalData",getTotalData(start, end));
        resultMap.put("rank",getRank(start, end));
        resultMap.put("risk",getRisk(start, end));
        resultMap.put("logistics",getLogistics(start, end));
        resultMap.put("stockChange",getStockChange(start, end));
        return resultMap;
    }

    @Override
    public Map<String, Object> getAllOrderData(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        // 平均支付时长无
        resultMap.put("totalData",getOrderTotalData(start, end));
        resultMap.put("rate",getOrderRate(start, end));
        resultMap.put("chart1",getOrderChart1(start, end));
        resultMap.put("chart2",getOrderChart2(start, end));
        // 超时订单未计算
        resultMap.put("chart3",getOrderChart3(start, end));
        return resultMap;
    }

    @Override
    public Map<String, Object> getDashBoardData(){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("todayData",dataMapper.getTodayData());
        resultMap.put("rank",dataMapper.getMostSaleBook(null, null));
        resultMap.put("chart",dataMapper.getTotalPricePerMonth(null, null));
        return resultMap;
    }

    @Override
    public int getSum(String name) {
        return dataMapper.getSum(name);
    }

    public Map<String, Object> getTotalData(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("click",dataMapper.getTotalClick());
        resultMap.put("pay",dataMapper.getTotalPay(start, end));
        resultMap.put("praise",dataMapper.getTotalPraise(start, end));
        resultMap.put("bad",dataMapper.getTotalBad(start, end));
        resultMap.put("sales", dataMapper.getTotalSales(start, end));
        resultMap.put("stock",dataMapper.getTotalStock());
        return resultMap;
    }

    public Map<String, Object> getOrderTotalData(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("user",dataMapper.getTotalUser());
        resultMap.put("stock",dataMapper.getTotalStock());
        resultMap.put("order",dataMapper.getTotalOrder(start, end));
        resultMap.put("sold",dataMapper.getTotalSales(start, end));
        resultMap.put("payTime", "30min");
        resultMap.put("pay",dataMapper.getTotalPay(start, end));
        return resultMap;
    }

    public Map<String, Object> getOrderRate(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("clickToOrderRate",dataMapper.getClickToPayRate(start, end));
        resultMap.put("orderToPayRate",dataMapper.getOrderToPayRate(start, end));
        resultMap.put("orderToCancelRate",dataMapper.getOrderToCancelRate(start, end));
        return resultMap;
    }

    public Map<String, Object> getOrderChart1(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("ordered",dataMapper.getOrderPerMonth(start, end));
        resultMap.put("orderPrice",dataMapper.getTotalPricePerMonth(start, end));
        resultMap.put("orderNum",dataMapper.getNumsPerMonth(start, end));
        return resultMap;
    }

    public Map<String, Object> getOrderChart2(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("tradeFinished",dataMapper.getOrderByStatusPerMonth(4, start, end));
        resultMap.put("tradePrice",dataMapper.getPriceByStatusPerMonth(4, start, end));
        resultMap.put("tradeNum",dataMapper.getNumsByStatusPerMonth(4, start, end));
        return resultMap;
    }

    public Map<String, Object> getOrderChart3(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("tradeFinished",dataMapper.getOrderByStatusPerMonth(3, start, end));
        resultMap.put("tradePrice",dataMapper.getPriceByStatusPerMonth(3, start, end));
        resultMap.put("tradeNum",dataMapper.getNumsByStatusPerMonth(3, start, end));
        return resultMap;
    }

    public Map<String, Object> getRank(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("mostPurchaseCategory",dataMapper.getMostPurchaseCategory(start, end));
        resultMap.put("mostPraiseBook",dataMapper.getMostPraiseBook(start, end));
        resultMap.put("mostClickBook",dataMapper.getMostClickBook());
        resultMap.put("mostSaleBook",dataMapper.getMostSaleBook(start, end));

        return resultMap;
    }

    public Map<String, Object> getRisk(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("unShelveRate",dataMapper.getUnShelveRate(start, end));
        resultMap.put("praiseRate",dataMapper.getPraiseRate(start, end));
        resultMap.put("badRate",dataMapper.getBadRate(start, end));
        resultMap.put("evaluateRate",dataMapper.getEvaluateRate(start, end));
        return resultMap;
    }

    public Map<String, Object> getLogistics(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("logistics",dataMapper.getLogiByTime(start, end));
        return resultMap;
    }

    public Map<String, Object> getStockChange(String start, String end){
        Map<String, Object> resultMap = new LinkedHashMap<>(8);
        resultMap.put("x",dataMapper.getMonth());
        resultMap.put("y",dataMapper.getLineData2(start, end));
        return resultMap;
    }
}
